/* test programma voor de finctie ai::seed_graph 
   input: 2 locaties met x,y en rotatie          
   op basis hiervan worden stap kostprijzen berekend
   rond de own & wanted locatie

*/












#include <stdio.h>
#define FIELD_X	15
#define FIELD_Y 15
#define ABS(a) ((a)<0?-(a):a)
#define MIN(a, b)  (((a) < (b)) ? (a) : (b))
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))


int main(void)
{
    //variabelen
    int i,j;
    int field[FIELD_X][FIELD_Y];
    int i_start,i_end;
    int j_start,j_end;
    int temp;

    int wanted_x,wanted_y,wanted_rotation;
    int own_x,own_y,own_rotation;




    //coordinaten in lokale vars steken
    wanted_x = 0;
    wanted_y = 0;
    wanted_rotation = 270;

    own_x = 3;
    own_y = 3;
    own_rotation = 315; 

	
    //Filling the simplefied field with 99
    for(i=0; i<FIELD_X; i++)
    {
        for(j=0; j<FIELD_X; j++)
        {
            field[i][j] = 99;
        }
    }



    //calculation the upper left corner and lower right corner
    // upper_left = (i_start,j_start) lower right = (i_end,j_end)

    i_start = MIN(own_x,wanted_x);
    j_start = MIN(wanted_y,own_y);

    i_end = MAX(own_x,wanted_x);
    j_end = MAX(wanted_y,own_y);


    //Rechthoek arround the wanted and own location
    for(i=i_start; i<=i_end; i++)
    {
        for(j=j_start; j<=j_end; j++)
        {
            field[i][j] = 1;
        }
    }




    //calculation the upper left corner and lower right corner
    // upper_left = (i_start,j_start) lower right = (i_end,j_end)

    i_start = MIN(own_x,wanted_x);
    j_start = MIN(wanted_y,own_y);

    i_end = MAX(own_x,wanted_x);
    j_end = MAX(wanted_y,own_y);



    //The next blob of code gives a cost value to the 3 squares arround the own_location
    //The code is designed to  handle edge cases

    //checking if the upper left corner of the "rechthoek" is an own location
    if( own_x == i_start && own_y == j_start )
    {
		
         if( (own_y - wanted_y) != 0)
            field[own_x+0][own_y+1] = ABS(0 - own_rotation)/45+1;          //right sideways

         if( (own_x - wanted_x) != 0)
            field[own_x+1][own_y+0] = ABS(270 - own_rotation)/45+1;        //downward

         if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
            field[own_x+1][own_y+1] = ABS(315 - own_rotation)/45+1;        //cross right down
    }
    //cheking if the lower left corner of the "rechthoek" is an own or wanted location.
    else if( own_x == i_end && own_y == j_start)
    {
        if( (own_x - wanted_x) != 0)
            field[own_x-1][own_y-0] = ABS(90 - own_rotation)/45+1;      //upward

        if( (own_y - wanted_y) != 0)
            field[own_x+0][own_y+1] = ABS(0 - own_rotation)/45+1;       //right sideways

        if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
            field[own_x-1][own_y+1] = ABS(45 - own_rotation)/45+1;      //cross right up
    }
    //cheking if the upper right corner of the "rechthoek" is an own or wanted location.
    else if( own_x == i_start && own_y == j_end)
    {
        //adjust the 3 squares arround this corner with a "cost" value
        if( (own_x - wanted_x) != 0)
            field[own_x+1][own_y-0] = ABS(270 - own_rotation)/45+1;          //downward

        if( (own_y - wanted_y) != 0)
            field[own_x-0][own_y-1] = ABS(180 - own_rotation)/45+1;          //left sideways

        if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
            field[own_x+1][own_y-1] = ABS(225 - own_rotation)/45+1;          //cross left down
    }
    //cheking id the lower right corner of the "rechthoek" is an own or wanted location.
    else if ( own_x == i_end && own_y == j_end)
    {
        //adjust the 3 squares arround this corner with a "cost" value
        if( (own_x - wanted_x) != 0)
            field[own_x-1][own_y-0] = ABS(90 - own_rotation)/45;           //upward

        if( (own_y - wanted_y) != 0)
            field[own_x-0][own_y-1] = ABS(180 - own_rotation)/45;          //left sideways

        if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
            field[own_x-1][own_y-1] = ABS(135 - own_rotation)/45;          //cross left up
    }






    //The next blob of code gives a cost value arround the wanted_location
    //The code is designed to  handle edge cases

    //checking if the upper left corner of the "rechthoek" is an wanted location
    if( wanted_x == i_start && wanted_y == j_start )
    {
	if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
	{
	    field[wanted_x+1][wanted_y+1] = ( ABS(315 - wanted_rotation)%180 ) /45+1;          //cross right down
	    if( ABS(own_x - wanted_x) >= 3 && ABS(own_y - wanted_y) >= 3 )
		field[wanted_x+2][wanted_y+2] = field[wanted_x+1][wanted_y+1]*field[wanted_x+1][wanted_y+1];
	}

        if( (own_x - wanted_x) != 0)
	{
            field[wanted_x+1][wanted_y] = (ABS(270 - wanted_rotation)%180)/45+1;           //downward
	    if( ABS(own_x - wanted_x) >= 3)
	    {
		field[wanted_x+2][wanted_y-0] = field[wanted_x+1][wanted_y-0]*field[wanted_x+1][wanted_y-0]; 	
	  	field[wanted_x+2][wanted_y+1] = (field[wanted_x+2][wanted_y+2] + field[wanted_x+2][wanted_y-0])/2+1;
	    }
	}
        if( (own_y - wanted_y) != 0)
	{
            
	    field[wanted_x-0][wanted_y+1] = (ABS(0 - wanted_rotation)%180)/45+1;          //right sideways	
	    if( ABS(own_y - wanted_y) >= 3)
	    {
		field[wanted_x-0][wanted_y+2] = field[wanted_x-0][wanted_y+1]*field[wanted_x-0][wanted_y+1];
		field[wanted_x+1][wanted_y+2] = (field[wanted_x+2][wanted_y+2] + field[wanted_x-0][wanted_y+2])/2+1;
	    }
	}

    }
    //cheking if the lower left corner of the "rechthoek" is an wanted location
    else if( wanted_x == i_end && wanted_y == j_start)
    {
	if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
	{
	    field[wanted_x-1][wanted_y+1] = ( ABS(135 - wanted_rotation)%180 ) /45+1;          //cross right up
	    if( ABS(own_x - wanted_x) >= 3 && ABS(own_y - wanted_y) >= 3 )
		field[wanted_x-2][wanted_y+2] = field[wanted_x-1][wanted_y+1]*field[wanted_x-1][wanted_y+1];
	}

        if( (own_x - wanted_x) != 0)
	{
            field[wanted_x-1][wanted_y-0] = (ABS(90 - wanted_rotation)%180)/45+1;           //upward
	    if( ABS(own_x - wanted_x) >= 3)
	    {
		field[wanted_x-2][wanted_y-0] = field[wanted_x-1][wanted_y-0]*field[wanted_x-1][wanted_y-0]; 	
	  	field[wanted_x-2][wanted_y+1] = (field[wanted_x-2][wanted_y+2] + field[wanted_x-2][wanted_y-0])/2+2;
	    }
	}
        if( (own_y - wanted_y) != 0)
	{
            
	    field[wanted_x-0][wanted_y+1] = (ABS(180 - wanted_rotation)%180)/45+1;          //right sideways	
	    if( ABS(own_y - wanted_y) >= 3)
	    {
		field[wanted_x-0][wanted_y+2] = field[wanted_x-0][wanted_y+1]*field[wanted_x-0][wanted_y+1];
		field[wanted_x-1][wanted_y+2] = (field[wanted_x-2][wanted_y+2] + field[wanted_x-0][wanted_y+2])/2+2;
	    }
	}
    }
    //cheking if the upper right corner of the "rechthoek" is an wanted location
    else if( wanted_x == i_start && wanted_y == j_end)
    {

	
        if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
	{
	    field[wanted_x+1][wanted_y-1] = ( ABS(225 - wanted_rotation)%180 ) /45+1;          //cross left down
	    if( ABS(own_x - wanted_x) >= 3 && ABS(own_y - wanted_y) >= 3 )
		field[wanted_x+2][wanted_y-2] = field[wanted_x+1][wanted_y-1]*field[wanted_x+1][wanted_y-1];
	}

        if( (own_x - wanted_x) != 0)
	{
            field[wanted_x+1][wanted_y-0] = (ABS(270 - wanted_rotation)%180)/45+1;           //downard
	    if( ABS(own_x - wanted_x) >= 3)
	    {
		field[wanted_x+2][wanted_y-0] = field[wanted_x+1][wanted_y-0]*field[wanted_x+1][wanted_y-0]; 	
	  	field[wanted_x+2][wanted_y-1] = (field[wanted_x+2][wanted_y-2] + field[wanted_x+2][wanted_y-0])/2+1;
	    }
	}
        if( (own_y - wanted_y) != 0)
	{
            
	    field[wanted_x-0][wanted_y-1] = (ABS(180 - wanted_rotation)%180)/45+1;          //left sideways	
	    if( ABS(own_y - wanted_y) >= 3)
	    {
		field[wanted_x-0][wanted_y-2] = field[wanted_x-0][wanted_y-1]*field[wanted_x-0][wanted_y-1];
		field[wanted_x+1][wanted_y-2] = (field[wanted_x+2][wanted_y-2] + field[wanted_x][wanted_y-2])/2+1;
	    }
	}

    }
    //cheking id the lower right corner of the "rechthoek" is an wanted location
    else if ( wanted_x == i_end && wanted_y == j_end)
    {
	if( (own_x - wanted_x) != 0 && (own_y - wanted_y) != 0)
	{
	    field[wanted_x-1][wanted_y-1] = ( ABS(135 - wanted_rotation)%180 ) /45+1;          //cross left up
	    if( ABS(own_x - wanted_x) >= 3 && ABS(own_y - wanted_y) >= 3 )
		field[wanted_x-2][wanted_y-2] = field[wanted_x-1][wanted_y-1]*field[wanted_x-1][wanted_y-1];
	}

        if( (own_x - wanted_x) != 0)
	{
            field[wanted_x-1][wanted_y-0] = (ABS(90 - wanted_rotation)%180)/45+1;           //upward
	    if( ABS(own_x - wanted_x) >= 3)
	    {
		field[wanted_x-2][wanted_y-0] = field[wanted_x-1][wanted_y-0]*field[wanted_x-1][wanted_y-0]; 	
	  	field[wanted_x-2][wanted_y-1] = (field[wanted_x-2][wanted_y-2] + field[wanted_x-2][wanted_y])/2+1;
	    }
	}
        if( (own_y - wanted_y) != 0)
	{
            
	    field[wanted_x-0][wanted_y-1] = (ABS(180 - wanted_rotation)%180)/45+1;          //left sideways	
	    if( ABS(own_y - wanted_y) >= 3)
	    {
		field[wanted_x-0][wanted_y-2] = field[wanted_x-0][wanted_y-1]*field[wanted_x-0][wanted_y-1];
		field[wanted_x-1][wanted_y-2] = (field[wanted_x-2][wanted_y-2] + field[wanted_x][wanted_y-2])/2+1;
	    }
	}

    }



    //Give robot location value 0 and wanted location value 0
    //Good for testing this function out
    field[own_x][own_y] = 0;
    field[wanted_x][wanted_y] = 0 ;


//////////////////////
for(i=0; i<FIELD_X; i++)
    {
        for(j=0; j<FIELD_X; j++)
        {
            printf(" %02d",field[i][j]);
        }
	printf("\n");
    }



};



